﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using SISGEZATBZVisual.BusinessEntities;

namespace SISGEZATBZVisual.Repository
{
    public class MatriculaRepository:MasterRepository,IMatriculaRepository
    {

        public void AddMatricula(Matricula matricula)
        {

            if (matricula.Alumno.Id == 0)
            {
                Context.Entry(matricula.Alumno).State = EntityState.Added;
            }
            else
            {
                Context.Entry(matricula.Alumno).State = EntityState.Unchanged;
            }


                Context.Entry(matricula.Seccion).State = EntityState.Modified;
                Context.Entry(matricula.AñoAcademico).State = EntityState.Unchanged;
 
                if (matricula.Alumno.Apoderado.Id == 0)
                {
                    Context.Entry(matricula.Alumno.Apoderado).State = EntityState.Added;
                }
                else
                {
                    Context.Entry(matricula.Alumno.Apoderado).State = EntityState.Unchanged;
                }

            Context.Matriculas.Add(matricula);
            Context.SaveChanges();

            Context.Entry(matricula.Alumno).State = EntityState.Modified;
            Context.SaveChanges();

        

        }


        public Matricula GetMatricula(int idAño, int idAlumno)
        {
            //var query = Context.Matriculas.Where(m => m.IdAñoAcademico.Equals(idAño));
            var query =
                from m in Context.Matriculas
                where m.IdAñoAcademico == idAño && m.IdAlumno == idAlumno
                select m;

            return query.SingleOrDefault();
        }


        public IEnumerable<Matricula> GetMatriculas(string alumno, int? idAñoAcademico)
        {

            var anio = Int32.Parse(idAñoAcademico.ToString());
            var query = from m in Context.Matriculas.Include("Seccion").Include("Seccion.Grado").Include("Seccion.Grado.Nivel").Include("AñoAcademico").Include("Alumno").Include("Alumno.Apoderado")
                        select m;

            query = from p in query
                    where p.AñoAcademico.Id.Equals(anio)
                    select p;

            if (!String.IsNullOrEmpty(alumno))
            {
                query = from p in query
                        where p.Alumno.Nombres.Contains(alumno)
                        select p;
            }


            var matriculas = new List<Matricula>();

            foreach (var m in query)
            {
               matriculas.Add(m);
            }


            return matriculas;

        }


        public Matricula GeMatricula(int idMatricula)
        {


            var query = from m in Context.Matriculas.Include("Seccion").Include("Seccion.Grado").Include("Seccion.Grado.Nivel").Include("AñoAcademico").Include("Alumno").Include("Alumno.Apoderado")
                        select m;

            query = from p in query
                    where p.Id.Equals(idMatricula)
                    select p;

            var query2 = query;

            return query2.SingleOrDefault();


        }


        public void UpdateMatricula(Matricula matricula)
        {
            //--------seccion alumno año academico
            //seccion grado nivel
            //alumno apoderado 
            //año academico


            Context.Entry(matricula.Alumno).State = EntityState.Modified;
            Context.Entry(matricula.Alumno.Apoderado).State = EntityState.Modified;
            Context.Entry(matricula.Seccion).State = EntityState.Modified;
            Context.Entry(matricula.Seccion.Grado).State = EntityState.Modified;
            Context.Entry(matricula.Seccion.Grado.Nivel).State = EntityState.Modified;

            Context.Entry(matricula.AñoAcademico).State = EntityState.Modified;

            Context.Entry(matricula).State = EntityState.Modified;
            Context.SaveChanges();

       
        }


 
    }
}
